package org.system.core.service.system;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.system.core.domain.entity.SysConfig;
import org.system.core.exception.ServiceException;
import org.system.core.mapper.system.SysConfigMapper;
import org.system.core.utils.MessageUtils;
import org.system.core.utils.StringUtils;

@Service
public class SysConfigService {

    @Autowired
    private SysConfigMapper sysConfigMapper;

    public SysConfig selectConfigByUserId(Long userId) {
        return sysConfigMapper.selectConfigByUserId(userId);
    }

    public void insertConfig(SysConfig sysConfig) {
        if (sysConfig.getUserId() == null || sysConfigMapper.insertConfig(sysConfig) <= 0) {
            throw new ServiceException(MessageUtils.message("system.monitor.add_error"));
        }
    }

    public void updateConfig(SysConfig sysConfig) {
        if (StringUtils.isNull(sysConfig.getUserId())) {
            throw new ServiceException(MessageUtils.message("system.user.no_id"));
        }
        SysConfig userConfig = selectConfigByUserId(sysConfig.getUserId());
        if (userConfig == null && sysConfigMapper.insertConfig(sysConfig) <= 0) {
            throw new ServiceException(MessageUtils.message("system.monitor.update_error"));
        }
        if (userConfig != null && sysConfigMapper.updateConfig(sysConfig) <= 0) {
            throw new ServiceException(MessageUtils.message("system.monitor.update_error"));
        }
    }

}
